package com.ruoyi.web.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.FaceDetection;
import com.ruoyi.system.service.IFaceDetectionService;
import com.ruoyi.web.controller.Util.FileUploadUtil;
import com.ruoyi.web.controller.Util.HttpUtils;
import com.ruoyi.web.controller.Util.ImageToBase64Utils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.params.HttpParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.HashMap;

import java.util.Map;

@Controller
@RequestMapping("/system/face")
public class FaceRecognitionController {
    @Autowired
    private IFaceDetectionService faceDetectionService;

    @PostMapping("/face")
    @ResponseBody
    public AjaxResult getmessage(@RequestParam("headImage") MultipartFile headImage,
                                 @RequestParam("number") String number,
                                 @RequestParam("faceId") String faceIds) {
        String[] faceId = faceIds.split(",");
        ImageToBase64Utils imageTool = new ImageToBase64Utils();
        FileUploadUtil fileUploadUtil = new FileUploadUtil();
        String fileReturnPath = fileUploadUtil.updateFileReturnPath(headImage);
        String base64 = imageTool.NetImageToBase64(fileReturnPath);
        String host = "https://zfa.market.alicloudapi.com";
        String path = "/efficient/idfaceIdentity";
        String method = "POST";
        String appcode = "686d16ee7c0649c9b8956be7d60acdf9";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("Threshold", "0.33");
        bodys.put("base64Str", base64);
        bodys.put("liveChk", "0");
        Long userId = Long.parseLong(faceId[0]);
        String name = faceDetectionService.selectFaceDetectionById(userId).getUsername();
        bodys.put("name", name);
        bodys.put("number", number);


        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            String str = EntityUtils.toString(response.getEntity());
            System.out.println(str);
            str = str.substring(str.indexOf("Validate_Result"), str.indexOf("Validate_Result") + 19);
            str = str.replace(",", "");
            str = str.replace("Validate_Result\":", "");
            System.out.println(str);
            for (String id : faceId) {
                FaceDetection faceDetection = new FaceDetection();
                faceDetection.setId(Long.parseLong(id));
                faceDetection.setResultStatus(str);
                faceDetectionService.updateFaceDetection(faceDetection);
            }
            return AjaxResult.success(str);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }

    }
}
